package com.BlogPark.service.ArticleService.Impl;

import com.BlogPark.common.utis.IdUtils;
import com.BlogPark.mapper.ArticleMapper;
import com.BlogPark.pojo.Article;
import com.BlogPark.pojo.DetailsUser;
import com.BlogPark.service.ArticleService.ArticleService;
import com.BlogPark.service.EditPersonalInformationService.EditPersonalInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.List;

/**
 * @Auther: MFM
 * @Date: 2022/8/10 - 08 - 10 - 21:32
 * @version: 1.0
 */

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;
    @Autowired
    EditPersonalInformationService editPersonalInformationService;

    @Override
    public Integer deleteArticleById(String id) {

        Integer integer = articleMapper.deleteArticleById(id);

        return integer;
    }

    @Override
    public boolean publishArticle(Article article) {
        article.setDate(new Date(System.currentTimeMillis()));
        article.setId(IdUtils.generateArticleId(article.getAutherId()));
        boolean flag = articleMapper.saveArtice(article);

        System.out.println(article.getId());

        return flag;
    }

    @Override
    public Integer updateArticle(Article article) {

        Integer integer = articleMapper.updateAritice(article);
        return integer;
    }

    @Override
    public List<Article> selectAllArticle(int userId) {

        List<Article> articles = articleMapper.selectByUserId(userId);
        return articles;
    }

    @Override
    public List<Article> selectAllArticle() {
        List<Article> articles = articleMapper.selectAllArticle();
        if (articles.size()>0){
            for (Article article : articles) {
                DetailsUser user = editPersonalInformationService.ViewPersonalInfo(article.getAutherId());
                article.setAutherName(user.getName());
            }
        }
        return articles;
    }
}
